<template>
    <div>
        <ExtForm ref="ExtForm" :formfields="formfields" @changeEventHandler="changeEventHandler" @clickEventHandler="clickEventHandler"></ExtForm>
        <drEmpSelect ref="singleEmpSelect" @onSelect="onSingleEmpSelect"></drEmpSelect>
        <empTransfer ref="empTransfer" @getSelected="onMoreEmpSelect"></empTransfer>
    </div>
</template>
<script>

import pageSetting from '@/components/lt/codeGenerator/pageSetting'
import Vue from 'vue'
import ExtForm from '@/components/lt/codeGenerator/components/ExtForm'
import flowSetting from './flowSetting'
import defaultSettings from '@/config/defaultSettings'
import drEmpSelect from '@/views/system/dr/drEmpSelect'
import empTransfer from '@/views/system/emp/empTransfer'

export default {
    name:'taskAttribute',
    components:{ExtForm,drEmpSelect,empTransfer},
    props:{
        attribute:{
           type:Object,
           default:()=>{}
        },
    },
    watch:{
        
    },
    data(){
        return{
            pageSetting,
            formfields:[
                {field:'id',label:'内码',component:pageSetting.常量.表单组件类型.单行文本,initialValue:this.attribute.id,disabled:true},
                {field:'type',label:'类型',component:pageSetting.常量.表单组件类型.单行文本,initialValue:this.attribute.type,disabled:true},
                {field:'label',label:'名称',component:pageSetting.常量.表单组件类型.单行文本,initialValue:this.attribute.label,rules: [{ required: true, message: '不能为空!' }]},
                {field:'desc',label:'节点描述',component:pageSetting.常量.表单组件类型.多行文本,initialValue:this.attribute.desc || '',rules: [{ required: false, message: '不能为空!' }]},
                {field:'beforeExecution',label:'执行前事件',component:pageSetting.常量.表单组件类型.单行文本,initialValue:this.attribute.beforeExecution || '',rules: [{ required: false, message: '不能为空!' }]},
                {field:'afterExecution',label:'执行后事件',component:pageSetting.常量.表单组件类型.单行文本,initialValue:this.attribute.afterExecution || '',rules: [{ required: false, message: '不能为空!' }]},
                // {field:'handleMode',label:'办理模式',component:pageSetting.常量.表单组件类型.单选框,initialValue:this.attribute.handleMode || flowSetting.constant.单人办理,options:flowSetting.dict.handleMode,rules: [{ required: true, message: '不能为空!' }]},
                {field:'passMode',label:'通过模式',component:pageSetting.常量.表单组件类型.单选框,initialValue:this.attribute.passMode || flowSetting.constant.一人通过即可通过,options:flowSetting.dict.passMode,rules: [{ required: true, message: '不能为空!' }]},
                {field:'assigneeMode',label:'人员指派模式',component:pageSetting.常量.表单组件类型.单选框,initialValue:this.attribute.assigneeMode || flowSetting.constant.动态指派,options:flowSetting.dict.assigneeMode,rules: [{ required: true, message: '不能为空!' }]},
                {field:'assigneeEmpName',label:'人员选择',hide:true,component:pageSetting.常量.表单组件类型.输入选择框,initialValue:this.attribute.assigneeEmpName || '',rules: [{ required: false, message: '不能为空!' }]},
                {field:'assigneeRole',label:'角色选择',hide:true,component:pageSetting.常量.表单组件类型.下拉选择,initialValue:this.attribute.assigneeRole || '',dictCode:defaultSettings.数据字典.角色,rules: [{ required: false, message: '不能为空!' }]},
                {field:'templateForm',label:'表单路径',component:'input',rules: [{required: false, message: '不能为空!'}],initialValue:this.attribute.templateForm || ''},
                {field:'pageId',label:'自定义页面内码',component:'input',rules: [{required: false, message: '不能为空!'}],initialValue:this.attribute.pageId || ''},
                
                
                // {field:'opinion',label:'是否需填写审批意见',component:pageSetting.常量.表单组件类型.开关,checkedChildren:true,unCheckedChildren:true,initialValue:this.attribute.opinion || true,rules: [{ required: true, message: '不能为空!' }]},
                // {field:'approval',label:'是否需要审批',component:pageSetting.常量.表单组件类型.开关,checkedChildren:true,unCheckedChildren:true,initialValue:this.attribute.approval || true,rules: [{ required: true, message: '不能为空!' }]},

                // {field:'effectiveTime',label:'审批有效时间(小时)',component:pageSetting.常量.表单组件类型.数字输入框,initialValue:this.attribute.effectiveTime || flowSetting.constant.有效时间,rules: [{ required: true, message: '不能为空!' }]},
                // {field:'timeoutMode',label:'审批超时模式',component:pageSetting.常量.表单组件类型.单选框,initialValue:this.attribute.timeoutMode || flowSetting.constant.自动通过,options:flowSetting.dict.timeoutMode,rules: [{ required: true, message: '不能为空!' }]},

                
            ],
            assigneeEmpNameArr:[],
            assigneeEmpNoArr:[]
        }
    },
   
    created(){
        this.initAttribute()
    },
    methods:{
        initAttribute(){
            // if(!this.attribute["handleMode"]){
            //     this.attribute["handleMode"]=flowSetting.constant.单人办理
            // }
            if(!this.attribute["passMode"]){
                this.attribute["passMode"]=flowSetting.constant.一人通过即可通过
            }
            if(!this.attribute["assigneeMode"]){
                this.attribute["assigneeMode"]=flowSetting.constant.动态人员
            }
            // if(!this.attribute["timeoutMode"]){
            //     this.attribute["timeoutMode"]=flowSetting.constant.自动通过
            // }
            // if(!this.attribute["effectiveTime"]){
            //     this.attribute["effectiveTime"]=flowSetting.constant.有效时间
            // }
            
            // if(!this.attribute["opinion"]){
            //     this.attribute["opinion"]=true
            // }
            // if(!this.attribute["approval"]){
            //     this.attribute["approval"]=true
            // }
            
            this.setHowHideByAssigneeMode(this.attribute["assigneeMode"])
        },
        changeEventHandler(item){
            if(item["assigneeMode"]){
                this.attribute["assigneeEmpNo"] =  ''
                this.attribute["assigneeEmpName"] = ''
                this.attribute["assigneeRole"] = ''
                this.setHowHideByAssigneeMode(item.assigneeMode)
            }
            for (const key in item) {
                this.attribute[key]=item[key]
                // Vue.set(this.attribute,key,item[key])
            }
            this.$emit("changeEventHandler",item)
            
        },
        /**
         * 根据人员指派模式，动态隐藏和显示
         */
        setHowHideByAssigneeMode(val){
            if(val==flowSetting.constant.指定角色){
                this.formfields[8].hide=true
                this.formfields[9].hide=false
                this.$nextTick(() => {
                    this.$refs["ExtForm"].getForm().setFieldsValue({'assigneeRole':''})
                })
                
            }else if(val==flowSetting.constant.指定人员){
                this.formfields[8].hide=false
                this.formfields[9].hide=true
                this.$nextTick(() => {
                    this.$refs["ExtForm"].getForm().setFieldsValue({"assigneeEmpName":''})
                })
                
            }else{
                this.formfields[8].hide=true
                this.formfields[9].hide=true
            }
        },
        clickEventHandler(field,form){
            if("assigneeEmpName"==field){
                this.$refs.empTransfer.edit()
                // if(this.attribute["handleMode"]==flowSetting.constant.单人办理){
                //     this.$refs["singleEmpSelect"].edit({})
                // }else{
                //     this.$refs.empTransfer.edit()
                // }
            }
            
        },
        onSingleEmpSelect(record){
            this.attribute["assigneeEmpName"] = record.empName
            this.attribute["assigneeEmpNo"] = record.empNo
            this.$refs["ExtForm"].getForm().setFieldsValue({"assigneeEmpName":record.empName})
        },
        onMoreEmpSelect(dataSource){
            let that = this
            if(!dataSource || dataSource.length==0){
                that.$message.warning("请先选择人员信息");
                return false;
            }
            if(that.attribute["assigneeEmpName"]){
                that.assigneeEmpNameArr.push(that.attribute["assigneeEmpName"])
            }

            if(that.attribute["assigneeEmpNo"]){
                that.assigneeEmpNoArr.push(that.attribute["assigneeEmpNo"])
            }
            dataSource.forEach(function (item) {
                that.assigneeEmpNameArr.push(item.empName)
                that.assigneeEmpNoArr.push(item.empNo)
            })
            that.assigneeEmpNameArr = Array.from(new Set(that.assigneeEmpNameArr))
            that.assigneeEmpNoArr = Array.from(new Set(that.assigneeEmpNoArr))
            that.attribute["assigneeEmpNo"] =  that.assigneeEmpNoArr.join(',')
            that.attribute["assigneeEmpName"] = that.assigneeEmpNameArr.join(',')
            that.$refs["ExtForm"].getForm().setFieldsValue({"assigneeEmpName":that.attribute["assigneeEmpName"]})
        }
    }
}
</script>